package de.gof.ppstool.staticdata.ui;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.gof.ppstool.staticdata.IStaticDataVerwaltung;
import de.gof.ppstool.staticdata.StaticDataNotFoundException;
import de.gof.ppstool.staticdata.db.StaticData;

@ManagedBean(name = StaticDataVerwaltungControllerBean.NAME)
@SessionScoped
public class StaticDataVerwaltungControllerBean implements Serializable {
	private static final long serialVersionUID = -4518206090724126891L;
	public static final String NAME = "sdv";
	private static final Logger LOGGER = LoggerFactory
			.getLogger(StaticDataVerwaltungControllerBean.class);
	@SuppressWarnings("unused")
	private static final boolean DEBUG = LOGGER.isDebugEnabled();
	private String aktuellePeriode;
	final String JSF_CURRENTPERIOD = "/staticdata/staticData.xhtml";
	@ManagedProperty(value = "#{" + IStaticDataVerwaltung.NAME + "}")
	private IStaticDataVerwaltung sdv;

	public StaticDataVerwaltungControllerBean() {
		super();
	}

	public IStaticDataVerwaltung getSdv() {
		return sdv;
	}

	public void setSdv(IStaticDataVerwaltung sdv) {
		this.sdv = sdv;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((sdv == null) ? 0 : sdv.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (obj == null)
			return false;

		if (getClass() != obj.getClass())
			return false;

		StaticDataVerwaltungControllerBean other = (StaticDataVerwaltungControllerBean) obj;
		if (sdv == null) {
			if (other.sdv != null)
				return false;

		} else if (!sdv.equals(other.sdv))
			return false;

		return true;
	}

	@Override
	public String toString() {
		return "StaticDataVerwaltungControllerBean [sdv=" + sdv + "]";
	}

	public void setAktuellePeriode(String aktuellePeriode) {
		this.aktuellePeriode = aktuellePeriode;
	}

	public String getAktuellePeriode() {
		return aktuellePeriode;
	}

	public String periodeAbfragen() {
		try {
			String aktuellePeriode = sdv.getCurrentPeriod();
			if ("-1".equals(aktuellePeriode)) {
				aktuellePeriode = "nicht gesetzt";
			}

			this.aktuellePeriode = aktuellePeriode;
		} catch (StaticDataNotFoundException e) {
			// TODO Exception fangen
			e.printStackTrace();
		}

		return JSF_CURRENTPERIOD;
	}

	public String periodeSetzen() {
		StaticData staticData;
		try {
			staticData = sdv.findStaticData(new Long(1));
			staticData.setPeriod(aktuellePeriode);
			sdv.changeStaticData(staticData);
		} catch (StaticDataNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return JSF_CURRENTPERIOD;
	}

}
